package com.ciwong.epaper.modules.epaper.adapter;


import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.epaper.bean.FollowReadWordDetailBean;
import com.ciwong.epaper.modules.epaper.bean.FollowReadWordDetails;
import com.ciwong.epaper.modules.epaper.ui.ChooseWordActivity;

import java.io.IOException;
import java.util.List;

public class ChooseWordGridAdapter extends BaseAdapter {

    private FollowReadWordDetailBean followReadWordDetailBean;
    private Context mContext;
    private MediaPlayer mediaPlayer;
    private ImageView word_audios_img_fake;
    //判断是自动停止播放 还是手动点击
    private int playCount;
    private ChooseWordExAdapter adapter;

    public ChooseWordGridAdapter(FollowReadWordDetailBean followReadWordDetailBean, Context mContext, MediaPlayer mediaPlayer, ChooseWordExAdapter adapter) {
        this.followReadWordDetailBean = followReadWordDetailBean;
        this.mContext = mContext;
        this.mediaPlayer = mediaPlayer;
        this.adapter = adapter;
    }

    @Override
    public int getCount() {
        return 0 == followReadWordDetailBean.getFollowReadWordDetailsList().size() ? 0 : followReadWordDetailBean.getFollowReadWordDetailsList().size();
    }

    @Override
    public Object getItem(int i) {
        return null == followReadWordDetailBean.getFollowReadWordDetailsList() ? null : followReadWordDetailBean.getFollowReadWordDetailsList().get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        if (null == view){
            view = View.inflate(mContext, R.layout.activity_choose_word_grid_item, null);
        }
        final CheckBox choose_work_content = (CheckBox) view.findViewById(R.id.choose_work_content);
        final List<FollowReadWordDetails> followReadWordDetails = followReadWordDetailBean.getFollowReadWordDetailsList();
        final int checkCount = followReadWordDetailBean.getCheckCount();
        final int wordCount = followReadWordDetails.size();
        if (wordCount == checkCount) {
            choose_work_content.setChecked(true);
        } else if (0 == checkCount){
            choose_work_content.setChecked(false);
        }else {
            if (null == followReadWordDetails.get(i).getIsChecked() || followReadWordDetails.get(i).getIsChecked()) {
                choose_work_content.setChecked(true);
            } else {
                choose_work_content.setChecked(false);
            }
        }
        TextView word_name_text = (TextView) view.findViewById(R.id.word_name_text);
        final ImageView word_audios_img = (ImageView) view.findViewById(R.id.word_audios_img);
        word_name_text.setText(followReadWordDetailBean.getFollowReadWordDetailsList().get(i).getWords());
        choose_work_content.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (!compoundButton.isPressed()) {
                    return;
                }
                if (b) {
                    int checkCount = followReadWordDetailBean.getCheckCount();
                    choose_work_content.setChecked(true);
                    followReadWordDetailBean.setCheckCount(checkCount + 1);
                    followReadWordDetailBean.getFollowReadWordDetailsList().get(i).setIsChecked(true);
                    if (mContext instanceof ChooseWordActivity){
                        ((ChooseWordActivity)mContext).setChooseWordDetail();
                    }
                    if (followReadWordDetailBean.getCheckCount() == 1) {
                        adapter.setCheckStatus(true);
                    } else if (followReadWordDetailBean.getCheckCount() == wordCount) {
                        adapter.setCheckStatus(true);
                    }

                } else {
                    int checkCount = followReadWordDetailBean.getCheckCount();
                    followReadWordDetailBean.setCheckCount(checkCount - 1);
                    choose_work_content.setChecked(false);
                    followReadWordDetailBean.getFollowReadWordDetailsList().get(i).setIsChecked(false);
                    if (mContext instanceof ChooseWordActivity){
                        ((ChooseWordActivity)mContext).setChooseWordDetail();
                    }
                    if (followReadWordDetailBean.getCheckCount() == wordCount - 1) {
                        adapter.setCheckStatus(false);
                    } else if (followReadWordDetailBean.getCheckCount() == 0) {
                        adapter.setCheckStatus(false);
                    }
                }
            }
        });

        word_audios_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // AudioPlayer.getInstance().play(dkWorkDetail.getAudios(),null);

                if (mediaPlayer != null && mediaPlayer.isPlaying()) {
                    playCount = 1;
                    stopPlay(word_audios_img);
                    startPlay(word_audios_img, followReadWordDetailBean.getFollowReadWordDetailsList().get(i));
                } else {
                    startPlay(word_audios_img, followReadWordDetailBean.getFollowReadWordDetailsList().get(i));
                }
            }

        });
        return view;
    }

    /**
     * 停止播放音频
     */
    private void stopPlay(ImageView word_audios_img) {
        Log.e("MediaPlayer ", "停止播放");
        if (playCount == 1) {
            stopPlayAnimation(word_audios_img_fake);
        } else {
            stopPlayAnimation(word_audios_img);
        }
        playCount = 0;
        mediaPlayer.stop();
        mediaPlayer.release();
        mediaPlayer = null;
    }

    /**
     * 开始播放音频
     */
    private void startPlay(final ImageView word_audios_img, FollowReadWordDetails followReadWordDetails) {
        startPlayAnimation(word_audios_img); // 开始线程，更新进度条的刻度
                            /*handler.postDelayed(startRunnable, 500);
                            // 开始线程，停止播放
                            handler.postDelayed(stopRunnable,
                                     300);*/
        try {
            word_audios_img_fake = word_audios_img;
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setDataSource(followReadWordDetails.getWordFile());
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mediaPlayer.prepareAsync();
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

                @Override
                public void onPrepared(MediaPlayer mp) {
                    Log.e("MediaPlayer ", "开始播放");
                    mediaPlayer.start();
                }
            });
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mediaPlayer) {
                    Log.e("MediaPlayer ", "停止播放");
                    stopPlayAnimation(word_audios_img);
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 开始播放动画
     */
    private void startPlayAnimation(ImageView imageView) {
        imageView
                .setImageResource(R.drawable.dk_bg_annimation);

        final AnimationDrawable animationDrawable = (AnimationDrawable) imageView
                .getDrawable();
        animationDrawable.start();
    }

    /**
     * 停止播放动画
     */
    public void stopPlayAnimation(ImageView word_audios_img) {
        Drawable drawable = word_audios_img.getDrawable();
        if (drawable != null && drawable instanceof AnimationDrawable) {
            ((AnimationDrawable) drawable).stop();
        }
        word_audios_img.setImageResource(R.mipmap.dk_icon_sound_3);
    }


}
